#!/bin/bash
echo '-----------设定预定义参数------------'
ROOT="/data/publish"
FILE="$ROOT/LLama.Web"
IMAGE="llama-web"
CONTAINER="llama-web"
GIT_URL="https://gitee.com/xie-bing/LLamaSharp.git"
BRANCH="master"
NETWORK="llama-network"

PUSHREMOTE="registry.cn-hangzhou.aliyuncs.com"



echo '设定环境变量'
# Apollo_AppId='gateway-ui-adminservice'
# Apollo_MetaServer='http://192.168.10.78:8080'

echo ''-----------开始部署'-----------'
if [ ! -d $ROOT ];then
   mkdir -p $ROOT
fi
cd $ROOT

if [ -d $FILE ]
then
    echo '文件夹存在'
    cd $FILE
    echo '1.正在停止容器'
    docker-compose down
    echo '2.删除代码目录'
    rm -rf $FILE
    echo '删除代码目录 正在停止容器 success'
    sleep 1
else
    echo '文件夹不存在'
    # shellcheck disable=SC2046
    docker stop $(docker ps -a  |grep $CONTAINER | awk -F " " '{print $1}')
    # shellcheck disable=SC2046
    docker rm $(docker ps -a  |grep $CONTAINER | awk -F " " '{print $1}')
    docker network rm $NETWORK
    rm -rf $FILE
    echo '手动停止删除容器成功'
fi


echo '删除已废弃的 images'
  docker stop $(docker ps -a  |grep $CONTAINER | awk -F " " '{print $1}')
  docker rm $(docker ps -a  |grep $CONTAINER | awk -F " " '{print $1}')
  #docker rmi $(docker images  |grep none | awk -F " " '{print $3}')
  #docker rmi $(docker images  |grep none | awk -F " " '{print $3}')

echo ''-----------开始拉取代码'-----------'

cd $ROOT
git clone $GIT_URL $FILE
cd  $FILE
git checkout $BRANCH
echo '==============git clone success================'
sleep 1

echo ''-----------开始构建'-----------'

docker-compose build
echo '==============build success================'

echo ''-----------开始推送镜像'-----------'
docker login -u谢汉冰 -pxie@2023..  $PUSHREMOTE
docker tag $IMAGE:latest  $PUSHREMOTE/$IMAGE:latest
docker  push $PUSHREMOTE/$IMAGE:latest

echo '==============push success================'


docker-compose up  -d

echo '==============run success================'


docker system prune -f

echo '=============prune success================='
